// pages/report/report.js
import util from '../../utils/util'

Page({

  data: {
    type:"", // 举报类型 0举报活动，1举报活动发布者
    reasonText:"", //举报的理由
    reasonImage:[], //举报的附加图片
    id:"",// 活动id
    openid:"", //发布者openid
  },

  onLoad(options) {
    // 举报活动就传值活动ID，举报发布者就传值发布者openid
    console.log("举报类型",options);
    if(options.type === "0"){
      this.setData({
        type:0,
        id:options.id
      })
    }else{
      this.setData({
        type:options.type,
        openid:options.openid
      })
    }

  },

  onShow(){
    console.log("加载")
    if(wx.getStorageSync("isLogin")){

    }else{
      // 没有登录 需要提醒登录了，才可以进行投诉举报
      wx.showModal({
        title: '提示',
        content: '请先登录之后才可以进行投诉举报哦~',
        showCancel: false,
        confirmText: '我知道了',
        confirmColor: '#3CC51F',
        success: (result) => {
          if (result.confirm) {
            wx.switchTab({
              url: '/pages/mine/mine',
            });
          }
        },
      });
    }
  },

  // 获取举报的文字理由
  getReasonText(e){
    console.log(e.detail.value);
    this.setData({
      reasonText:e.detail.value
    })
  },

  // 添加图片,最多三张
  addReasonImage(){
    let {reasonImage} = this.data;
    const that = this;
    console.log("图片上传");
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (result) => {
        const temp = result.tempFilePaths[0].split('.');
        console.log(temp)
        wx.cloud.uploadFile({
          cloudPath:`${util.wxuuid()}.png`,
          filePath:result.tempFilePaths[0],
          success:res=>{
            console.log("上传成功",res.fileID);
            reasonImage.push(res.fileID);
            that.setData({
              reasonImage
            })
          },
        });
      },
    });
  },

  // 删除图片
  deleteImg(e){
    let {reasonImage} = this.data;
    // console.log("删除图片",e.currentTarget.dataset.url);
    const url = e.currentTarget.dataset.url;
    const index = reasonImage.findIndex(item=>item===url);
    reasonImage.splice(index,1);
    wx.cloud.deleteFile({
      fileList:[url],
      success:res=>{
        console.log("删除图片成功");
      },
    })
    this.setData({
      reasonImage
    })
  },
  // 提交
  submit(){
    wx.showLoading({
      title: "提交中",
      mask: true,
    });
      
    let {reasonText,reasonImage,type,id,openid} = this.data;
    if(reasonText === ""){
      wx.showModal({
        title: '提示',
        content: '请输入投诉举报理由',
        showCancel: false,
        confirmText: '好的',
        confirmColor: '#3CC51F',
      });
      return;
    }

    wx.cloud.callFunction({
      name:"reportReason",
      data:{
        reasonText,
        reasonImage,
        type,
        id,
        openid
      },
      success:res=>{
        console.log("上传成功",res)
        wx.hideLoading();
        wx.switchTab({
          url: '/pages/index/index',
        });
      },
      fail:err=>{
        console.log("上传失败",err);
        wx.hideLoading()
      }
    })

  }
 

})